package com.dbgs.insurance.insuredperson.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;

import com.dbgs.insurance.insuredperson.R;
import com.dbgs.insurance.insuredperson.constants.ConstStr;
import com.dbgs.insurance.insuredperson.fragments.HomeFragment;
import com.dbgs.insurance.insuredperson.fragments.MyFragment;
import com.dbgs.insurance.insuredperson.fragments.NewsFragment;
import com.dbgs.insurance.insuredperson.permissions.IPermission;
import com.dbgs.insurance.insuredperson.service.MyLocationService;
import com.githang.statusbar.StatusBarCompat;
import com.hss.base.BaseActivty;
import com.tbruyelle.rxpermissions.RxPermissions;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Subscriber;

public class MainActivity extends BaseActivty implements IPermission {
    private final Class[] fragments = {
            HomeFragment.class, NewsFragment.class, MyFragment.class,

    };

    int tabIndex = 0;

    String[] tabNameArrSelect = null;
    String[] tabNameArr = null;
    RadioButton[] radioButtons = new RadioButton[3];

    @BindView(R.id.rg_tab)
    RadioGroup rgTab;
    @BindView(R.id.realtabcontent)
    FrameLayout realtabcontent;
    @BindView(android.R.id.tabcontent)
    FrameLayout tabcontent;
    @BindView(android.R.id.tabhost)
    FragmentTabHost tabhost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        initListener();
        rgTab.check(R.id.radiobtn_main_home_page);
        checLocationPermission();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        if (Build.VERSION.SDK_INT > 11) {
        } else {
            super.onSaveInstanceState(outState);
        }

    }

    @Override
    public void initView() {
        super.initView();
        initTabBar();
    }

    @Override
    public void initListener() {
        super.initListener();
        rgTab.setOnCheckedChangeListener(changeListener);
    }

    void initTabBar() {
        MainTabHelper tabHlper = new MainTabHelper(getApplicationContext());

        tabhost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        TabHost.TabSpec tabSpec = null;
        RadioButton radiobtn = null;
        Typeface iconfont = Typeface.createFromAsset(getAssets(), "iconfont/iconfont.ttf");
        String[] titleArr = new String[]{getString(R.string.str_main_home_page),
                getString(R.string.str_main_info), getString(R.string.str_main_mine)};
        for (int i = 0; i < titleArr.length; i++) {
            // 为每一个Tab按钮设置图标、文字和内容
            tabSpec = tabhost.newTabSpec(i + "").setIndicator(i + "");
            // 将Tab按钮添加进Tab选项卡中
            Bundle data = new Bundle();
            data.putInt(ConstStr.KEY_TYPE, i + 1);
            tabhost.addTab(tabSpec, fragments[i], data);
            radiobtn = tabHlper.getTabRadioBtn(i, null);
            radiobtn.setTypeface(iconfont);
            radiobtn.setText(getTabNameArr()[i]);//getTabNameArr()[i]
            rgTab.addView(radiobtn);
            radioButtons[i] = radiobtn;
        }

    }

    RadioGroup.OnCheckedChangeListener changeListener = new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub

            RadioButton radiobtn = (RadioButton) group.findViewById(checkedId);
            switch (checkedId) {
                case R.id.radiobtn_main_home_page:
                    tabIndex = 0;
                    StatusBarCompat.setStatusBarColor(MainActivity.this, getResources().getColor(R.color.color_main), true);
                    break;
                case R.id.radiobtn_main_infor:
                    StatusBarCompat.setStatusBarColor(MainActivity.this, getResources().getColor(R.color.color_main), true);
                    tabIndex = 1;
                    break;
                case R.id.radiobtn_main_min:
                    StatusBarCompat.setStatusBarColor(MainActivity.this, getResources().getColor(R.color.color_main), true);
                    tabIndex = 2;
                    break;
            }
            tabhost.setCurrentTab(tabIndex);
//            setTabText(tabIndex);
        }
    };

    String[] getTabNameArr() {
        if (null == tabNameArr) {
            tabNameArr = getResources().getStringArray(R.array.arr_nav_iconfonts);
        }
        return tabNameArr;
    }

    RxPermissions rxPermissions; // where this is an Activity instance

    @Override
    public RxPermissions getPermission() {
        if (rxPermissions == null) {
            rxPermissions = new RxPermissions(this); // where this is an Activity instance
        }
        return rxPermissions;
    }

    /**
     * 启用百度定位
     */
    void startLocationService() {
        Intent location = new Intent(this, MyLocationService.class);
        startService(location);
    }

    void stopLocationService() {
        Intent location = new Intent(this, MyLocationService.class);
        startService(location);
    }

    /**
     * 检查位置需要的权限
     */
    public void checLocationPermission() {
        getPermission().request(
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.ACCESS_NETWORK_STATE
        )
                .subscribe(new Subscriber<Boolean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Boolean aBoolean) {//AssessQuestionFragment
                        if (aBoolean) {
                            startLocationService();
                        } else {
                            showToast("请打开定位相关的权限");
                        }

                    }
                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopLocationService();
    }

}
